package repository

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Slice"
	"go-fox-edge-kernel-manager/component"
)

type repoLocalAppLoadService struct {
}

func (e *repoLocalAppLoadService) ExtendStartConfig(serviceIniFileInfoList []map[string]interface{}) error {
	configValue, err := component.ManageConfigService.GetConfigValueThis("serviceStartConfig")
	if err != nil {
		return err
	}

	// 从配置信息中，取出启动列表信息
	serviceList := Map.GetOrDefault(configValue, "services", make([]interface{}, 0))

	//  组织数据
	appName2ConfigStatus := make(map[string]interface{})
	for _, value := range serviceList.([]interface{}) {
		service := value.(map[string]interface{})

		appName := Map.GetString(service, "appName", "")
		appName2ConfigStatus[appName] = service
	}

	loadConfigName := "appLoad"
	for _, serviceIniFileInfo := range serviceIniFileInfoList {
		//根据fileName取出shell的基本数据
		appName := Map.GetString(serviceIniFileInfo, "appName", "")
		if appName == "" {
			continue
		}

		//根据fileName取出启动配置的扩展数据，如果没有则默认为true
		config := appName2ConfigStatus[appName]
		if config != nil {
			load := Map.GetBool(config.(map[string]interface{}), "appLoad", false)
			serviceIniFileInfo[loadConfigName] = load
		} else {
			serviceIniFileInfo[loadConfigName] = true
		}
	}
	return nil
}

func (e *repoLocalAppLoadService) QueryServiceLoad(appName, appType string, defaultValue bool) bool {
	configValue, err := component.ManageConfigService.GetConfigValueThis("serviceStartConfig")
	if err != nil {
		return false
	}

	if len(configValue) == 0 {
		return defaultValue
	}

	// 从配置信息中，取出启动列表信息
	value := configValue["services"]
	if value == nil {
		return defaultValue
	}

	serviceList := Map.GetOrDefault(configValue, "services", make([]interface{}, 0))

	//  组织数据
	appName2ConfigStatus := make(map[string]interface{})
	for _, v := range serviceList.([]interface{}) {
		service := v.(map[string]interface{})

		name := Map.GetString(service, "appName", "")
		appName2ConfigStatus[name] = service
	}

	configStatus := appName2ConfigStatus[appName]
	if configStatus == nil {
		return defaultValue
	}

	return Map.GetBool(configStatus.(map[string]interface{}), "appLoad", false)
}

func (e *repoLocalAppLoadService) SaveServiceLoad(appName, appType string, appLoad bool) error {
	// 简单校验参数
	if Method.HasEmpty(appName, appType) || Method.HasNull(appLoad) {
		return errors.New("参数不能为空:appName, appType, appLoad")
	}

	iniFileInfoList := LocalAppConf.GetConfFileInfoList()
	e.ExtendStartConfig(iniFileInfoList)

	for _, data := range iniFileInfoList {
		if appName != data["appName"] || appType != data["appType"] {
			continue
		}
		if appType == "kernel" {
			continue
		}

		// 获得启动配置
		configValue, err := component.ManageConfigService.GetConfigValueThis("serviceStartConfig")
		if err != nil {
			return err
		}

		// 从配置信息中，取出启动列表信息
		serviceList := make([]map[string]interface{}, 0)
		val := configValue["services"]
		if val != nil {
			serviceList = Slice.ToMapList(val.([]interface{}))
		}

		// 组织陈MAP，简化查询
		appName2ConfigStatus := Map.BuildMapByStringKey(serviceList, "appName")

		//根据fileName取出启动配置的扩展数据，如果没有则默认为true
		config := Map.ComputeIfAbsent(appName2ConfigStatus, appName, make(map[string]interface{}))
		config["appName"] = appName
		config["appLoad"] = appLoad

		serviceList = Map.BuildValues(appName2ConfigStatus, make(map[string]interface{}))
		configValue["services"] = serviceList
		component.ManageConfigService.SaveConfigValueThis("serviceStartConfig", configValue)
	}

	return nil
}
